package tbpay

import (
	"bytes"
	"encoding/base64"
)

func EncryptData(data map[string]interface{}, key []byte) (string, error) {
	jsonStr := sortParamsJson(data)
	ciphertext, err := EncryptAES([]byte(jsonStr), key)
	if err != nil {
		return "", err
	}

	// 返回 Base64 编码后的密文
	return base64.StdEncoding.EncodeToString(ciphertext), nil
}

func pkcs7Padding(data []byte, blockSize int) []byte {
	padding := blockSize - len(data)%blockSize
	padtext := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(data, padtext...)
}
